package com.iwhalecloud.bss.kite.cucc.order.accept.constructor.inst;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.order.accept.constructor.DataConstructorFactory;
import com.iwhalecloud.bss.kite.cucc.order.accept.constructor.IDataConstructor;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferResInstRelExt;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteMktResInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 销售品实例营销资源实例关系构造器
 * @version 1.0
 */
@Component
public class OfferResInstRelExtConstructor extends InstConstructor<KiteMktResInst, OfferResInstRelExt> {

    @Override
    public String getTableName() {
        return TableConsts.OfferResInstRelExt.getTableName();
    }

    @Override
    public OfferResInstRelExt build(OrderRequest orderRequest, String offerResInstRelId, Map<String, Object> params) {
        if (Objects.isNull(orderRequest) || StringUtils.isBlank(offerResInstRelId)) {
            return null;
        }

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteMktResInst kiteMktResInst = querySceneInstService.getMktResInst(orderRequest.getCustId(),
            offerResInstRelId);
        if (isIneffectiveInst(kiteMktResInst)) {
            return null;
        }

        // 营销资源的上级可能是接入产品实例或可选包销售品实例
        String prodInstId = "";
        String offerInstId = "";
        String compInstId = "";
        String ownerCustId = "";
        KiteProdInst kiteProdInst = querySceneInstService.getProdInstByOfferResInstRelId(orderRequest.getCustId(),
            offerResInstRelId);
        if (isEffectiveInst(kiteProdInst)) {
            prodInstId = kiteProdInst.getProdInstId();
            offerInstId = kiteProdInst.getOfferInstId();
            compInstId = kiteProdInst.getOfferInstId();
            ownerCustId = kiteProdInst.getOwnerCustId();

            params = Optional.ofNullable(params).orElse(new HashMap<>());
            params.put(KEY_PARENT_INST_TYPE, kiteProdInst.getInstType());
            params.put(KEY_PARENT_INST_ID, kiteProdInst.getProdInstId());
        }
        else {
            KiteOfferInst kiteOfferInst = querySceneInstService
                .getOfferInstByOfferResInstRelId(orderRequest.getCustId(), offerResInstRelId);
            if (isEffectiveInst(kiteOfferInst)) {
                offerInstId = kiteOfferInst.getOfferInstId();
                ownerCustId = kiteOfferInst.getOwnerCustId();
                InstType instType = kiteOfferInst.getInstType();
                if (Objects.equals(InstType.ADDITIONAL_OFFER_INST, instType)) {
                    compInstId = querySceneInstService.getAdditionalOfferInstCompInstId(orderRequest.getCustId(),
                        kiteOfferInst.getOfferInstId());
                }
                else {
                    compInstId = offerInstId;
                }

                params = Optional.ofNullable(params).orElse(new HashMap<>());
                params.put(KEY_PARENT_INST_TYPE, instType);
                params.put(KEY_PARENT_INST_ID, offerInstId);
            }
        }

        OfferResInstRelExt offerResInstRelExt = null;
        if (StringUtils.isNotBlank(prodInstId)) {
            kiteMktResInst.setUseProdInstId(prodInstId);
            kiteMktResInst.setCompInstId(offerInstId);
            offerResInstRelExt = convert(orderRequest, kiteMktResInst, params);
            InstContainer.ofRequest(orderRequest).addOfferResInstRelExt(offerResInstRelExt);

            IDataConstructor<String, ProdInst> dataConstructor = DataConstructorFactory
                .getInstConstructor(InstType.PROD_INST);
            if (!dataConstructor.contains(orderRequest, "", prodInstId, null)) {
                dataConstructor.build(orderRequest, prodInstId,
                    ImmutableMap.of(KiteInst.ACTION_TYPE, ActionType.M.name()));
            }
        }
        else if (StringUtils.isNotBlank(offerInstId)) {
            kiteMktResInst.setUseProdInstId("");
            kiteMktResInst.setOfferInstId(offerInstId);
            kiteMktResInst.setCompInstId(compInstId);
            offerResInstRelExt = convert(orderRequest, kiteMktResInst, params);
            InstContainer.ofRequest(orderRequest).addOfferResInstRelExt(offerResInstRelExt);

            IDataConstructor<String, OfferInst> dataConstructor = DataConstructorFactory
                .getInstConstructor(InstType.ADDITIONAL_OFFER_INST);
            if (!dataConstructor.contains(orderRequest, "", offerInstId, null)) {
                dataConstructor.build(orderRequest, offerInstId,
                    ImmutableMap.of(KiteInst.ACTION_TYPE, ActionType.M.name()));
            }
        }

        if (AcceptUtils.isAddInst(offerResInstRelExt) && StringUtils.isNotBlank(ownerCustId)) {
            offerResInstRelExt.setOwnerCustId(ownerCustId);
        }

        return offerResInstRelExt;
    }

    @Override
    public OfferResInstRelExt newInst(OrderRequest orderRequest, KiteMktResInst kiteMktResInst) {
        if (!ObjectUtils.allNotNull(orderRequest, kiteMktResInst)) {
            return null;
        }

        Date now = new Date();
        OfferResInstRelExt offerResInstRelExt = KiteBeanUtils.transform(kiteMktResInst, OfferResInstRelExt.class,
            KiteInst.ACTION_TYPE);
        offerResInstRelExt.setOwnerCustId(orderRequest.getCustId());
        offerResInstRelExt.setActionType(ActionType.A.name());
        if (StringUtils.isBlank(offerResInstRelExt.getMktResInstId())) {
            offerResInstRelExt.setMktResInstId(null);
        }
        offerResInstRelExt.setProdTypeCd(KeyConsts.PROD_TYPE_CD_01);
        offerResInstRelExt.setStatusDate(now);
        offerResInstRelExt.setStaffId(orderRequest.getStaffId());
        offerResInstRelExt.setLanId(orderRequest.getLanId());
//        offerResInstRelExt.setPropertyType(propertyType);
        offerResInstRelExt.setProdInstId(offerResInstRelExt.getOfferResInstRelId());

        setAttrs(offerResInstRelExt, kiteMktResInst.getAttrMap(),
            Sets.newHashSet(StringUtils.upperCase(TableConsts.OfferResInstRelExt.getTableName()),
                StringUtils.upperCase(TableConsts.OfferResInstRel.getTableName())));

        return offerResInstRelExt;
    }
}
